package dbTools.db.writer.writers;

import java.util.Map;

import dbTools.tableInfo.field.Field;
import dbTools.tableInfo.info.TableInfo;

public class OracleWriter extends AbstractSQLWriter{
	/**
	 * 获取建表语句头部
	 * @param tableInfo 表定义信息
	 * @return 建表语句头部
	 */
	@Override
	public String getCreateHeaderInfo(TableInfo tableInfo){
//		return "BEGIN EXECUTE IMMEDIATE 'DROP TABLE " + tableInfo.getTableId()+"'; EXCEPTION WHEN OTHERS THEN NULL; END;\r\n"
//			 + "CREATE TABLE " + tableInfo.getTableId();
		return "DROP TABLE " + tableInfo.getTableId()+";\r\n"
		 + "CREATE TABLE " + tableInfo.getTableId();
	}
	
	/**
	 * 获取字段的建表信息
	 * @param field 字段信息
	 * @return 字段的建表信息
	 */
	@Override
	public String getFieldInfo(Field field){
		
		String length = "(" + field.getLengh() + ")";
		
		if(field.getDbDataType().toUpperCase().equals("INT")
		   ||field.getDbDataType().toUpperCase().equals("INTEGER")
		   ||field.getDbDataType().toUpperCase().equals("TINYINT")
		   ||field.getDbDataType().toUpperCase().equals("FLOAT")
		   ||field.getDbDataType().toUpperCase().equals("SMALLINT")
		   ||field.getDbDataType().toUpperCase().equals("MEDIUMINT")
		   ||field.getDbDataType().toUpperCase().equals("BIGINT")
		   ||field.getDbDataType().toUpperCase().equals("DOUBLE")
		   ||field.getDbDataType().toUpperCase().equals("DATE")
		   ||field.getDbDataType().toUpperCase().equals("TIME")
		   ||field.getDbDataType().toUpperCase().equals("YEAR")
		   ||field.getDbDataType().toUpperCase().equals("DATETIME")
		   ||field.getDbDataType().toUpperCase().equals("TIMESTAMP")
		   ){
			length = "";
		}
		
		String notNull = " NOT NULL ";
		
		if(field.isNotNull() == false){
			notNull = "";
		}
		
		String otherInfo = "";
		
		return field.getFieldName() + " " + field.getDbDataType() + length + notNull + " " + field.getDefaultValue() + " " + otherInfo;
	}

	/**
	 * 获取主键信息
	 * @param tableInfo 表定义信息
	 * @return 主键信息
	 */
	@Override
	public String getKeyInfo(TableInfo tableInfo){
		
		String keys ="";
		
		//key信息
		for(Map.Entry<String,Field> fieldEntry : tableInfo.getKeyFields().entrySet())
		 {
			 Field field=fieldEntry.getValue();
			 if(keys.length() == 0){
				 keys = field.getFieldName();
			 }else{
				 keys += "," + field.getFieldName();
			 }
		 } 

		return String.format("CONSTRAINT prk_%s Primary Key (%s)",tableInfo.getTableId(), keys) ;
		//return String.format("PRIMARY KEY   (%s)", keys) ;
	}
	
	/**
	 * 获取建表语句尾部
	 * @param tableInfo 表定义信息
	 * @return 建表语句尾部
	 */
	@Override
	public String getCreateFooterInfo(TableInfo tableInfo){
		StringBuilder sql = new StringBuilder();
		
		sql.append("    COMMENT ON TABLE "+tableInfo.getTableId()+" IS '" + tableInfo.getTableName()+"';\r\n");
		
		for(Map.Entry<String,Field> fieldEntry : tableInfo.getAllFields().entrySet())
		 {
			 Field field=fieldEntry.getValue();
			 sql.append("    COMMENT ON COLUMN "+tableInfo.getTableId()+"."+field.getFieldName()+" IS '" + field.getItemName()+"';\r\n");
		 } 
		
		return sql.toString();
	}

}
